package com.atguigu.springbootdemo.controller;

import com.atguigu.springbootdemo.bean.Employee;
import com.atguigu.springbootdemo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Smexy on 2023/1/7
 */
@RestController
public class EmployeeController
{

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/emp")
    public Object handle1(String op,Integer id,String lastname,String gender,String email){

        //把收到的参数封装为数据模型
        Employee e = new Employee(id, lastname, email, gender);

        switch (op){
            case "select": if (id == null){
                return "id非法";
            }else {
                //要查询的那个人不存在
                // 使用service层提供的方法
                Employee employee = employeeService.getById(id);
                // 可以使用Service中内置的Mapper提供的方法
                //Employee employee = employeeService.getBaseMapper().selectById(id);
                return employee == null ? "查无此人" : employee;
            }
            case "delete": if (id == null){
                return "id非法";
            }else {
                employeeService.removeById(id);
                return "ok";
            }
            case "insert": employeeService.save(e); return "ok";

            case "update": if (id == null){
                return "id非法";
            }else {
                employeeService.saveOrUpdate(e);
                return "ok";
            }
            default:  return "ok";
        }

    }

    @RequestMapping(value = "/getAllEmp")
    public Object handle2(){
        List<Employee> all = employeeService.list();
        return all;
    }


}
